<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../../header.jsp"%>

<div class="left ">
<div class="border bg">
<div class="page-header">
<h2>地区管理</h2>
<div class="cms_toolbar">
	<a href="javascript:;" onclick="goList()" class="btn"><i class="icon-list"></i>返回</a>
</div>
</div>

<div>
<form id="detailForm" class="form-horizontal">
<input type="hidden" id="id" name="id" value="${param.id}">
<fieldset>
	<div class="control-group">
		<label for="name"  class="control-label">名称：</label>
		<div class="controls">
		<input type="text" id="name" name="name" class="input" maxlength="50" /> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="parentId"  class="control-label">上级：</label>
		<div class="controls">
		<select name="parentId" id="parentId"></select> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="form-actions">
		<a id="saveBtn" href="javascript:void(0);" class="btn btn-primary" onclick="doSave()"><i class="icon-ok"></i>保存</a>
	</div>
</fieldset>
</form>
</div>

</div>
</div>

<script type="text/javascript">
function goList() {
	var url = "./cities.do?parentId=" + $("#parentId").val();
	goBack(url);
}

function doSave() {
	if (!detailValidator.form()) {
		return;
	}
	
	block("#detailForm");
	$.ajax({
		type: "POST",
		url: "save.json",
		data: $("#detailForm").serialize(),
		dataType: "json",
		success: saveSucceedCallback,
		error:function(message){
			unblock("#detailForm");
			$.alert("出错了，错误信息为：\n"+message);
		}
	});
}

function saveSucceedCallback(data) {
	unblock("#detailForm");
	if (!data.succeed) {
		$.notice(data.message);
		return;
	}

	$.info("保存成功！");
	if ($("#id").val() == "") {
		var url = "city.do?id=" + data.data.id;
		location.href = url;
	}
}

function loadDetail() {
	block("#detailForm");
	$.ajax({
		type: "GET",
		url: "detail.json",
		data: {id : $("#id").val()},
		dataType: "json",
		success: function(json) {
			unblock("#detailForm");
			$("#name").val(json.data.name);
			loadProvinces("parentId", function(){
				$("#parentId").val(json.data.parentId);
			});
		},
		error:function(message){
			unblock("#detailForm");
			$.alert("出错了，错误信息为：\n"+message);
		}
	});
}

var detailValidator;
$(document).ready(function() {
	detailValidator = $("#detailForm").validate({
		rules: {
			name:{required:true, maxlength:50 },
			parentId:{required:true}
		}
	});
	
	if($("#id").val() != ""){
		loadDetail();
	} else {
		loadProvinces("parentId", function(){
			$("#parentId").val(request("parentId"));
		});
	}
});


</script>


<%@ include file="../../../footer.jsp"%>